package cn.rxls.community.controller;

import cn.rxls.community.result.Result;
import cn.rxls.community.service.UploadFileService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 文件上传、删除
 */
@RestController
@RequestMapping("/upload")
@Validated
public class UploadController {

    @Resource
    UploadFileService uploadFileService;

    /**
     * 上传图片
     *
     * @param file 图片文件
     * @return 文件路径
     */
    @PostMapping("/img")
    public Result<List<String>> imageUploadFault(@RequestParam(value = "image_file", required = false)
                                                 @NotNull(message = "上传文件不能为空")
                                                 @Size(min = 1, max = 9, message = "文件数大于1并且小于10")
                                                 MultipartFile[] file) {
        return Result.success(uploadFileService.uploadImage(file));
    }


    /**
     * 删除图片文件
     *
     * @param paths 需要删除的文件名集合
     * @return 成功数
     */
    @PostMapping("/deleteFile")
    public Result<Integer> delFile(@RequestParam(value = "image_file", required = false)
                                   @NotNull(message = "文件名不能为空")
                                   @Size(min = 1, max = 100, message = "删除数大于1且小于100")
                                   List<String> paths) {
        return Result.success(uploadFileService.delImage(paths));
    }

}
